using B.S.RbacData.ErrorCode;
using ContentmanagementBaseRead.API.Applications.Commands.Departments;
using ContentmanagementBaseRead.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseRead.API.Controllers
{
    /// <summary>
    /// 科室管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DepartmentController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DepartmentController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public DepartmentController(IMediator mediator, ILogger<DepartmentController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有科室信息
        /// </summary>
        /// <param name="onlyFirstLevel">是否只获取一级科室</param>
        /// <param name="onlyEnabled">是否只获取启用的科室</param>
        /// <param name="parentId">父级ID</param>
        /// <returns>科室列表</returns>
        [HttpGet("all")]
        public async Task<ApiResult<List<DepartmentDtos>>> GetAllDepartments()
        {
            try
            {
                GetAllDepartmentsCommand command = new GetAllDepartmentsCommand();

                var result = await _mediator.Send(command);

                _logger.LogInformation("科室信息获取请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取科室信息请求处理异常");
                return new ApiResult<List<DepartmentDtos>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取科室信息请求处理异常：{ex.Message}",
                    data = new List<DepartmentDtos>()
                };
            }
        }

        /// <summary>
        /// 获取一级科室信息
        /// </summary>
        /// <returns>一级科室列表</returns>
        [HttpGet("first-level")]
        public async Task<ApiPaging<List<DepartmentDtos>>> GetFirstLevelDepartments([FromQuery] GetFirstLevelDepartmentsCommand command)
        {
            try
            {
               

                var result = await _mediator.Send(command);

                _logger.LogInformation("科室查询完成，返回数据条数：{Count}", result.data?.Count ?? 0);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取科室列表过程中发生异常，页码：{PageIndex}，每页数量：{PageSize}",
                    command.PageIndex, command.PageSize);

                return new ApiPaging<List<DepartmentDtos>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取科室列表异常：{ex.Message}",
                    data = new List<DepartmentDtos>(),
                    TotalCount = 0,
                    PageCount = 0,
                };
            }
        }
    }
} 